JavaScript Set Date Methods

    In the Previous JS tutorial, we learned about the JavaScript get methods, where we retrieve specific data from the Date object. In this tutorial, we will learn how to set the data value to a Date object using JavaScript date set methods.

    JS Date setFullYear() Method

    The setFullyear() method is used to change the Year value of a Date Object. The method also returns the time in milliseconds from date 1 Jan 1970.

    Syntax

    date_obj_name.setFullYear(YYYY)

    Example

    <script>
        let my_dob = new Date();
        my_dob.setFullYear(1999);
        console.log(my_dob); 
    </script>

    Output

    Thu Mar 25 1999 13:19:21 GMT+0530 (India Standard Time)

    The setFullYear(YYYY,MM, DD ) can also accept two optional parameters to set the month and day value for the date object.

    Example

    <script>
       let my_dob = new Date();
       my_dob.setFullYear(1999,09,04);
       console.log(my_dob); 
    </script>

    Output

    Mon Oct 04 1999 13:22:39 GMT+0530 (India Standard Time)

    JS Date setMonth() Method

    We can use the setMonth() method to set the month of a Date object. The method can accept a month numeric value from 0 to 11 where 0 represents January and 11 represents December. The method also returns the time in milliseconds from date 1 Jan 1970.

    Syntax

    date_obj.setMonth(MM)

    Example

    <script>
       let my_dob = new Date();
       my_dob.setMonth(09); //set october
       console.log(my_dob); 
    </script>

    Output

    Mon Oct 25 2021 13:43:06 GMT+0530 (India Standard Time)

    JS Date setDate() method

    The setDate() method can set the date number to the Date object. The date number must between 1 to 31.

    Syntax

    date_obj.setDate(DD)

    Example

    <script>
       let my_dob = new Date();
       my_dob.setDate(04);    // set date to 4
       console.log(my_dob); 
    </script>

    Output

    Thu Mar 04 2021 13:47:25 GMT+0530 (India Standard Time)

    JS Date setHours() method

    The setHours() method is used to set the hours value for a Date object. It can accept values between 0 to 23.

    Syntax

    date_obj.setHours(HH)

    Example

    <script>
       let my_dob = new Date();
       my_dob.setHours(12);     // set hours to 12 pm
       console.log(my_dob); 
    </script>

    Output

    Thu Mar 25 2021 12:50:37 GMT+0530 (India Standard Time)

    JS Date setMinutes() method

    The setMinutes() method can set the new minutes value for an existing date object. It can accept a numeric parameter value between 0 to 59.

    Syntax

    date_obj.setMinutes(MM)

    Example

    <script>
       let my_dob = new Date();
       my_dob.setMinutes(14);    // set minutes to 14
       console.log(my_dob); 
    </script>

    Output

    Thu Mar 25 2021 13:14:53 GMT+0530 (India Standard Time)

    JS Date setSeconds() Method

    If you want to change the seconds value for a Date object there you can use the Date setSeconds() method. It accepts a numeric parameter value between 0 to 59.

    Syntax

    date_obj.setSeconds(SS)

    Example

    <script>
       let my_dob = new Date();
       my_dob.setSeconds(20); //set seconds value to 20
       console.log(my_dob); 
    </script>

    Output

    Thu Mar 25 2021 13:55:20 GMT+0530 (India Standard Time)

    Date comparison

    We can use the comparison operators(>, <, ==) between two date objects in order to compare them. Let's compare the date of birth of two friends sam and john, and see who is older.

    Example

    <script>
        let sam_dob = new Date(1999, 0,13);
        let john_dob = new Date(1999,0,12);  
    
        if(sam_dob > john_dob)
        {
            console.log("John is older than Sam ")
        }
        else{
            console.log("Sam is older than John")
        }
    </script>

    Output

    John is older than Sam 

    Summary

    • The setDate() method sets the day for the date object(0-31).
    • The setFullYear() method sets the Year for the date object(YYYY), it can also accept two optional parameters month and day.
    • The setHours() method sets the Hours for the date object(0-59).
    • The setMilliSeconds() method sets the milliseconds for the date object(0-999).
    • The setMonth () method sets the month for the date object(0-11).
    • We can also use comparison operators between two data objects to compare them.